#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;

int main() {
    auto func = [](const vector<int>& v) -> void {
        for (auto e : v)
            cout << e;
        cout << endl;
    };

    auto cond = [](const vector<int>& v) -> bool {
        if (v[3] % 2 != 0)
            return false;
        if ((v[2] + v[3] + v[4]) % 3 != 0)
            return false;
        if (!(v[5] == 0 || v[5] == 5))
            return false;
        if ((100 * v[4] + 10 * v[5] + v[6]) % 7 != 0)
            return false;
        if ((100 * v[5] + 10 * v[6] + v[7]) % 11 != 0)
            return false;
        if ((100 * v[6] + 10 * v[7] + v[8]) % 13 != 0)
            return false;
        if ((100 * v[7] + 10 * v[8] + v[9]) % 17 != 0)
            return false;
        return true;
    };

    vector<int> v(10);
    vector<vector<int>> a;
    iota(v.begin(), v.end(), 0);
    do {
        if (cond(v))
            a.push_back(v);
    } while (next_permutation(v.begin(), v.end()));
    for (auto e : a)
        func(e);

    int c = 0;
    vector<int> r;
    for (int j = 9; j >= 0; --j) {
        for (int i = 0; i < a.size(); ++i) {
            c += a[i][j];
        }
        r.push_back(c % 10);
        c /= 10;
    }
    while (c) {
        r.push_back(c % 10);
        c /= 10;
    }
    
    func(r);
    reverse(r.begin(), r.end());
    func(r);
}